<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends Controller {
    private $pathUrl;
    public function __construct()
    {
        parent::__construct();
        if(empty($_SESSION['admin'])){
            $this->error('请先登录',U('Login/index'));
        }else{
            $admin_name = session('name');
            $this->assign('admin_name',$admin_name);
        }
        $this->upLevel();
        $this->managerPower();
    }

    public function _empty()
    {
        echo "404,找不到此页面";
    }

    function get_img_url($url, $basename, $size=250)
    {
        switch ($size) {
            case 350:
                $pre = '350_';
                break;
            case 250:
                $pre = '250_';
                break;
            case 150:
                $pre = '150_';
                break;
            case 50:
                $pre = '50_';
                break;
            case 30:
                $pre = '30_';
                break;
            default:
                $pre = null;
                break;
        }

        $imgpath = rtrim($url,'/').'/';
        $imgpath .= substr($basename,0,4).'/';
        $imgpath .= substr($basename,4,2).'/';
        $imgpath .= substr($basename,6,2).'/';
        $imgpath .= $pre .$basename;

        return $imgpath;
    }

    //党员账号升级
    function upLevel ()
    {
        //首先查询所有党员id name
        //根据党员id查看他的评论文章数目
        //在查看他的交党费次数
        //在查看他的捐款次数

        //首先查询所有党员id name
        $user = M("user");
        $userList = $user -> field("id,name,level,level_update_time")->order("id")->select();
//        echo "<pre>";
//        print_r($userList);
        foreach($userList as $val){
            //获取他的级别更新时间,如果实在本月时间内,则直接跳过
            $time = strtotime($val['level_update_time']);
            $startTime = strtotime(date("Y-m"));
            if($time>$startTime){
                continue;
            }

            $id = $val['id'];
            $level = $val['level'];
            if($level == 25){
                continue;
            }
            //根据党员id查看他的评论文章数目
            $comment = M("comment");
            $res = $comment -> field("count(id) total")->where("user_id=$id")->group("article_id")->select();
            $total = count($res);
            //如果发表的文章大于等于 你现在的级别 * 20 则继续
            $total_a = $level * 2;
            if($total<$total_a){
                continue;
            }
            //根据党员id查看那他的交党费次数
            $fee = M("fee");
            $res = $fee->field("count(id) total")->where("user_id=$id")->find();
            $total = $res['total'];
            $total_a = $level * 3;
            if($total<$total_a){
                continue;
            }
            //根据党员id查看那他的捐款次数
            $don = M("donation");
            $res = $don->field("count(id) total")->where("user_id=$id")->find();
            $total = $res['total'];
            $total_a = $level * 2;
            if($total<$total_a){
                continue;
            }

            //以上条件都满足后,就可以升级了;
            $level = $level+1;
            $data['level'] = $level;
            $data['level_update_time'] = date("Y-m-d H:i:s");
            $user ->where("id=$id")->save($data);

        }
    }

    //权限查询
    function managerPower()
    {
        //获取管理员id;
        $id = $_SESSION['admin']['id'];
        //根据管理员id查询他所拥有的权限
        $user = M("admin_user");
        $res = $user ->alias("au")
            ->join("dangjian_admin_user_sort aus on aus.id=au.sort_id")
            ->field("aus.power")
            ->where("au.id=$id")
            ->find();
        $power = explode(",",$res['power']);


        //查询出所有的权限
        $p = M("admin_user_power");
        $res = $p->field("id,path")->select();

        foreach($res as $key=>$value){
            if(in_array($value['id'],$power)){
                $path[] = strtolower($value['path']);
            }
        }
//        echo "<pre>";
//        print_r($path);
        $this->pathUrl = $path;
    }

    //权限比对
    public final function checkPower()
    {
        $url = strtolower(MODULE_NAME."/".CONTROLLER_NAME."/".ACTION_NAME);
//        echo $url;
//        echo "<pre>";
//        print_r($this->pathUrl);die;
        if(!in_array($url,$this->pathUrl)){
            $this->error("您没有权限执行此操作");
        }
    }
}
?>